1. Field of the Invention
The present invention relates to programming languages for computer systems. More specifically, the present invention relates to a method and an apparatus for facilitating use of an object-oriented enumerated type within a programming language.
2. Related Art
An enumerated type facility within a programming language allows a programmer to easily create types that include a fixed number of enumeration constants. For example, an enumerated type can define a fixed set of colors, such as RED, WHITE and BLUE. In general, an enumerated type can be used to represent many things, including natural enumerations (e.g., the four seasons) and program-specific enumerations (e.g., the states in a finite state machine).
Many programming languages, including C, C++, Ada and C# provide enumerated type facilities. However, in all of these facilities, enumerated types are merely thinly disguised integers. Some of these languages, including C++, Ada and C#, provide some measure of type safety and a few pieces of built-in functionality, such as automatic string conversion, but none allow for programmer extensibility.
Programmers in object-oriented languages sometimes implement more powerful enumerated types manually by writing classes that export self-typed constants to serve as enumeration constants. For example, item 21 in “Effective Java Programming”, by Joshua Bloch, pp. 104-114, Addison-Wesley 2001, discusses one such method of using a “Typesafe Enum” pattern.
While enumerated types implemented in this way are more powerful than traditional enumerated types, they have a number of drawbacks: (1) they require substantial programming effort to produce code to define the classes; (2) the additional code required to define the classes makes the code less readable; (3) the additional code may introduce errors into the program; and (4) enumerated types defined in this way are not usable in all contexts where traditional types are used. In particular, they may not be used in a multi-way branch statement (also referred to as a “switch statement” or a “case statement”).
What is needed is a method and an apparatus for providing powerful enumerated types without the drawbacks of involved in manually defining classes to support the enumerated types.